/*
 *  Copyright 2010 johan.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package nu.zoom.catonine.desktop.impl;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import nu.zoom.catonine.desktop.FileReopener;
import nu.zoom.catonine.prefs.Preferences;
import nu.zoom.swing.desktop.Workbench;
import nu.zoom.swing.desktop.common.BackendException;
import nu.zoom.swing.desktop.preferences.InvalidDataTypeException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * An implementation for re-opening previosuly opened files for tailing.
 * 
 * @author "Johan Maasing" &lt;johan@zoom.nu&gt;
 */
public class FileReopenerImpl implements FileReopener {

	private final Log log = LogFactory.getLog(getClass());
	private final Workbench workbench;
	private final Preferences preferences;
	private final Set<String> filenames = new HashSet<String>();

	public FileReopenerImpl(final Workbench workbench, final Preferences preferences) {
		this.workbench = workbench;
		this.preferences = preferences;
		try {
			filenames.addAll(preferences.getOpenedFiles());
		} catch (InvalidDataTypeException ex) {
			log.warn("Unable to retrieve the rememebered files", ex);
		} catch (BackendException ex) {
			log.warn("Unable to retrieve the rememebered files", ex);
		}
	}

	@Override
	public synchronized void remember(final File file) throws IOException {
		filenames.add(file.getCanonicalPath());
	}

	@Override
	public synchronized void forget(final File file) throws IOException {
		filenames.remove(file.getCanonicalPath());
	}

	@Override
	public synchronized Set<String> getRememeberedFiles() {
		return new HashSet<String>(filenames);
	}

	@Override
	public void initialize() throws Exception {
		// Plug in initialization
		workbench.addWorkBenchListener(this);
	}

	@Override
	public void start() {
		// We don't care about workbench start, application must explicitly call
		// reopen
	}

	@Override
	public void visible() {
	}

	@Override
	public void close() {
		Set<String> rememeberedFiles = getRememeberedFiles();
		log.debug("Rememebering: " + rememeberedFiles.size() + " filenames");
		try {
			preferences.rememeberOpenedFiles(rememeberedFiles);
		} catch (InvalidDataTypeException ex) {
			// Ignore, nothing we can do and nothing the user wants to know
			// about
			log.warn("Unable to save rememebered files", ex);
		} catch (BackendException ex) {
			// Ignore, nothing we can do and nothing the user wants to know
			// about
			log.warn("Unable to save rememebered files", ex);
		}
	}
}
